<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义构造函数实现</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">
    <style>
        body {
            font-family: "Arial", sans-serif;
            background-color: #f5f5f5;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        .container {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 600px;
            text-align: center;
        }

        h1 {
            color: #2c3e50;
            font-size: 24px;
        }

        .btn {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #2980b9;
        }

        .output {
            margin-top: 20px;
            padding: 20px;
            background-color: #ecf0f1;
            border-radius: 8px;
            color: #2c3e50;
            font-size: 18px;
        }

        .output:empty {
            display: none;
        }

        .code-block {
            background-color: #2d3436;
            color: white;
            padding: 15px;
            border-radius: 8px;
            font-family: "Courier New", monospace;
            margin: 20px 0;
            text-align: left;
            white-space: pre-wrap;
            word-wrap: break-word;
        }

        .code-block pre,
        .code-block code {
            border-radius: 5px;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>自定义构造函数 (myNew) 示例</h1>
        <button class="btn" onclick="runExample()">运行示例</button>

        <div class="output" id="output"></div>

        <div class="code-block">
            <p>自定义构造函数 myNew 的代码：</p>
            <pre>
<code class="language-js">
function myNew() {
  const o = Object.create({});
  const currentConstructor = Array.prototype.shift.call(arguments);
  o.__proto__ = currentConstructor.prototype;
  let res = currentConstructor.apply(o, arguments);
  return typeof res === 'object' ? res : o;
}
</code>
      </pre>
        </div>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
    <script>
        function myNew() {
            const o = Object.create({});
            const currentConstructor = Array.prototype.shift.call(arguments);
            o.__proto__ = currentConstructor.prototype;
            let res = currentConstructor.apply(o, arguments);
            return typeof res === 'object' ? res : o;
        }

        function Person(name, age) {
            this.name = name;
            this.age = age;
        }

        Person.prototype.sayHello = function () {
            return `你好，我叫 ${this.name}，今年 ${this.age} 岁。`;
        };

        function runExample() {
            const person1 = myNew(Person, "张三", 25);
            const outputDiv = document.getElementById('output');
            outputDiv.innerHTML = `
        创建的对象信息：<br>
        姓名: ${person1.name} <br>
        年龄: ${person1.age} <br>
        问候语: ${person1.sayHello()}
      `;
        }
        hljs.highlightAll();
    </script>

</body>

</html>